package cn.detu.ln.service.impl;

import cn.detu.ln.entity.Student;
import cn.detu.ln.mapper.StudentMapper;
import cn.detu.ln.service.StudentService;
import cn.detu.ln.utils.RestUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Override
    public RestUtil selectAll() {
        return new RestUtil(200,"",studentMapper.selectList(null));
    }


    @Override
    public Student queryStudentByNum(Long stuNum) {
        return studentMapper.selectOne(new QueryWrapper<Student>().eq("stu_num",stuNum));
    }

    @Override
    public RestUtil studentDelete(Long stuNum) {
        return studentMapper.deleteById(stuNum)>0? new RestUtil(200,"删除成功！",null):new RestUtil(500,"删除失败",null);
    }

    @Override
    public RestUtil studentUpdate(Student student) {
        return studentMapper.updateById(student)>0?new RestUtil(200,"修改成功！",null):new RestUtil(500,"修改失败",null);
    }

    @Override
    public RestUtil studentInsert(Student student) {
        return studentMapper.insert(student)>0?new RestUtil(200,"恭喜，该生录入成功！",null):new RestUtil(500,"添加失败",null);
    }

    @Override
    public RestUtil queryStudentByPage(Integer current, Integer rows, String search) {
        // 1.根据查询条件判断构造的操作数据库的模糊查询条件
        // 如果search有值的话，就进行分页模糊查询，没有值的话就进行普通查询
        QueryWrapper<Student> queryWrapper = (search != null && search != "")?
                new QueryWrapper<Student>().like("stu_name",search) : null;
        // 2.根据mybatis-plus分页查询返回Page对象，MP规定，实现分页查询必须规定一个分页拦截器才会生效
        Page<Student> studentPage = studentMapper.selectPage(new Page<Student>(current,rows),queryWrapper);
        // 3.选择一个合适的数据结构封装返回的数据 Map数据结构
        HashMap<String, Object> map = new HashMap<>();
        // 4.使用Page对象获取分页显示的信息
        map.put("total",studentPage.getTotal());// 总条数
        map.put("pages",studentPage.getPages());// 总页数
        map.put("students",studentPage.getRecords());// 当前页数据
        return studentPage != null ? new RestUtil(200,"",map) : null;

    }

    @Override
    public RestUtil queryStudentByStuNumPage(Integer current, Integer rows, String search) {
        // 1.根据查询条件判断构造的操作数据库的模糊查询条件
        // 如果search有值的话，就进行分页模糊查询，没有值的话就进行普通查询
        QueryWrapper<Student> queryWrapper = (search != null && search != "")?
                new QueryWrapper<Student>().like("stu_num",search) : null;
        // 2.根据mybatis-plus分页查询返回Page对象，MP规定，实现分页查询必须规定一个分页拦截器才会生效
        Page<Student> studentPage = studentMapper.selectPage(new Page<Student>(current,rows),queryWrapper);
        // 3.选择一个合适的数据结构封装返回的数据 Map数据结构
        HashMap<String, Object> map = new HashMap<>();
        // 4.使用Page对象获取分页显示的信息
        map.put("total",studentPage.getTotal());// 总条数
        map.put("pages",studentPage.getPages());// 总页数
        map.put("students",studentPage.getRecords());// 当前页数据
        return studentPage != null ? new RestUtil(200,"",map) : null;

    }

    @Override
    public RestUtil queryStudentByStuClassPage(Integer current, Integer rows, String search) {
        // 1.根据查询条件判断构造的操作数据库的模糊查询条件
        // 如果search有值的话，就进行分页模糊查询，没有值的话就进行普通查询
        QueryWrapper<Student> queryWrapper = (search != null && search != "")?
                new QueryWrapper<Student>().like("stu_class",search) : null;
        // 2.根据mybatis-plus分页查询返回Page对象，MP规定，实现分页查询必须规定一个分页拦截器才会生效
        Page<Student> studentPage = studentMapper.selectPage(new Page<Student>(current,rows),queryWrapper);
        // 3.选择一个合适的数据结构封装返回的数据 Map数据结构
        HashMap<String, Object> map = new HashMap<>();
        // 4.使用Page对象获取分页显示的信息
        map.put("total",studentPage.getTotal());// 总条数
        map.put("pages",studentPage.getPages());// 总页数
        map.put("students",studentPage.getRecords());// 当前页数据
        return studentPage != null ? new RestUtil(200,"",map) : null;

    }

    @Override
    public RestUtil login(Student student) {
        Student resUser = studentMapper.selectOne(new QueryWrapper<Student>().eq("stu_name",student.getStuName()));
        if(resUser == null ) return new RestUtil(500,"用户名不存在，请联系管理员！",null);
        // 用户名存在，判断密码是否正确
        return resUser.getStuPwd().equals(student.getStuPwd()) ?
                new RestUtil(200,"恭喜，登陆成功！",null) :
                new RestUtil(500,"密码错误，请重试！",null);
    }

    @Override
    public RestUtil studentUpdatePwd(Student student, String newPwd) {
        // 判断新密码与旧密码不能一样
        System.out.println(student.getStuPwd());
        System.out.println(newPwd);
        if(student.getStuPwd().equals(newPwd))  return new RestUtil(500,"新密码与旧密码不能一样，请重试！",null);
        student.setStuPwd(newPwd);
        return studentMapper.updateById(student) >0 ?new RestUtil(200,"恭喜，密码已修改成功！",null) : new RestUtil(500,
                "密码修改失败，请重试！",
                null);
    }

    @Override
    public RestUtil select(Long stuNum) {
        Student student = studentMapper.selectOne(new QueryWrapper<Student>().eq("stu_num",stuNum));
        if(student == null) return new RestUtil(500,"用户不存在！",null);
        return new RestUtil(200,"查询成功！",student);
    }
}
